package com.xlb.service.impl;

import com.xlb.common.Const;
import com.xlb.common.ServiceResponse;
import com.xlb.dao.UserMapper;
import com.xlb.pojo.User;

import com.xlb.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author:xiexy
 * @date: 2021/1/24 10:52 下午
 **/
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 登陆
     * @param username
     * @param password
     * @return
     */
    @Override
    public ServiceResponse<User> login(String username, String password) {
        //检查登陆名存不存在
        int resultCount =userMapper.checkUsername(username);
        if(resultCount == 0){
            return ServiceResponse.createByErrorMessage("用户名不存在");
        }

        // todo 密码登陆MD5(可拓展)

        User user = userMapper.selectLogin(username,password);
        if(user == null){
            return ServiceResponse.createByErrorMessage("密码错误");
        }

        user.setPassword(org.apache.commons.lang.StringUtils.EMPTY);
        return ServiceResponse.createBySuccess("登陆成功",user);
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @Override
    public ServiceResponse<String> register(User user){
        //检查登陆名存不存在
        int resultCount =userMapper.checkUsername(user.getUsername());
        if(resultCount > 0){
            return ServiceResponse.createByErrorMessage("用户名存在");
        }
        //检查手机号是否被注册
        resultCount=userMapper.checkPhone(user.getPhone());
        if(resultCount > 0){
            return ServiceResponse.createByErrorMessage("手机号存在");
        }
        //默认普通用户
        user.setpow(Const.Pow.ROLE_CUSTOMER);

        resultCount = userMapper.insert(user);
        if (resultCount == 0){
            return ServiceResponse.createByErrorMessage("注册失败");
        }
        return ServiceResponse.createBySuccessMessage("注册成功");
    }

    public ServiceResponse<String> checkValid(String str,String type){
        return null;
    }
}
